<?php

namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Request;

class Project extends Controller
{
    public function task(Request $request)
    {
        if ($request->isGet()) {
            return $this->fetch();
        }


        if ($request->isAjax()) {
            $map = [];
            $city = $request->param("city");
            $house = $request->param("house");
            $area = $request->param("area");
            $sort = $request->param("sort");
            $page = $request->param("page");


            if ($city != "不限") {
                $map['shi'] = ['=', $city];
            }
            if ($house != "不限") {

                $map['house_type'] = ['=', $house];

            }
            if ($area != "不限") {

                if ($area == "60平米以下") {
                    $map['area'] = ['<', '60'];
                }
                if ($area == "60-80平米") {
                    $map['area'] = ['between', '60,80'];
                }
                if ($area == "80-100平米") {
                    $map['area'] = ['between', '80,100'];
                }
                if ($area == "100平米以上") {
                    $map['area'] = ['>', '100'];
                }

            }

            if ($map) {
                if ($sort == "开工(早-晚)") {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->where($map)
                        ->order('start_time', 'asc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }

                } elseif ($sort == "开工(晚-早)") {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->where($map)
                        ->order('start_time', 'desc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }
                } else {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->where($map)
                        ->order('id', 'desc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }
                }
            } else {
                if ($sort == "开工(早-晚)") {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->order('start_time', 'asc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }
                } elseif ($sort == "开工(晚-早)") {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->order('start_time', 'desc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }
                } else {
                    $res = Db::name("project")->field('id,address,area,start_time,project_picture')
                        ->page($page, 12)
                        ->order('id', 'desc')
                        ->select();
                    foreach ($res as $index => $re) {
                        $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                        $res[$index]["client"] = $cli;
                    }
                }
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理 调整图片清晰度和比例 HYF
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!320x200r/gravity/Center/crop/320x200/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
            }
            return json($res);
        }

    }

    public function lists(Request $request)
    {
        $flag = $request->param("flag");
        if ($flag == 1) {
            $res = [["name" => "不限"], ["name" => "周口市"]];
            return json($res);
        }
        if ($flag == 2) {
            // $res=array("不限","一居室","二居室","三居室","四居室","其他");
            $res = [["name" => "不限"], ["name" => "一居室"], ["name" => "二居室"], ["name" => "三居室"], ["name" => "四居室"], ["name" => "其他"]];
            return json($res);
        }
        if ($flag == 3) {
            //$res=array("不限","60平米以下","60-80平米","80-100平米","100平米以上");
            $res = [["name" => "不限"], ["name" => "60平米以下"], ["name" => "60-80平米"], ["name" => "80-100平米"], ["name" => "100平米以上"]];
            return json($res);
        }
        if ($flag == 4) {
            //$res=array("不限","60平米以下","60-80平米","80-100平米","100平米以上");
            $res = [["name" => "默认排序"], ["name" => "开工(早-晚)"], ["name" => "开工(晚-早)"]];
            return json($res);
        }

    }

    public function detail(Request $request)
    {
        //获取该项目id
        $id = $request->param("id");
        $qiniu_config = cmf_get_option('qiniu');
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";

        if ($request->isGet()) {
            $pro = Db::name('project')
                ->where('id', $id)->find();

                $cli = Db::name('project_client')->field("name")->where('project_id', $id)->select();
                $pro["client"] = $cli;

            $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
            if (!$pm['avatar']) {
                $pm['avatar'] = "/static/images/photo.png";
            } else {
                $pm['avatar'] = $qiniu_config['domain'] . '/' . $pm['avatar'] . $q_thumb;
            }
            $am = Db::name('user')->where('id', $pro['customer_manager_id'])->find();
            if (!$am['avatar']) {
                $am['avatar'] = "/static/images/photo.png";
            } else {
                $am['avatar'] = $qiniu_config['domain'] . '/' . $am['avatar'] . $q_thumb;
            }
            $ds = Db::name('user')->where('id', $pro['designer_id'])->find();
            if (!$ds['avatar']) {
                $ds['avatar'] = "/static/images/photo.png";
            } else {
                $ds['avatar'] = $qiniu_config['domain'] . '/' . $ds['avatar'] . $q_thumb;
            }
            $qc = Db::name('user')->where('id', $pro['qualityl_id'])->find();
            if (!$qc['avatar']) {
                $qc['avatar'] = "/static/images/photo.png";
            } else {
                $qc['avatar'] = $qiniu_config['domain'] . '/' . $qc['avatar'] . $q_thumb;
            }


            $stage = Db::name('project_stage')->field('title,status')
                ->where('project_id', $id)->select();
            $sd=0;
            $cc=0;
            $wg=0;
            $yg=0;
            $mz=0;
            $bj=0;
            foreach ($stage as $index => $s) {
//                dump($s['title']);
//                dump($s['status']);
               if($s['title']=="水电工程"&&$s['status']==2){
                   $sd=2;
               }
               if($s['title']=="水电工程"&&$s['status']!=2){
                   $sd=1;
               }
               if($s['title']=="拆除工程"&&$s['status']==2){
                   $cc=2;
               }
               if($s['title']=="拆除工程"&&$s['status']!=2){
                   $cc=1;
               }
               if($s['title']=="瓦工工程"&&$s['status']==2){
                    $wg=2;
                }
                if($s['title']=="瓦工工程"&&$s['status']!=2){
                    $wg=1;
                }
                if($s['title']=="油工工程"&&$s['status']==2){
                    $yg=2;
                }
                if($s['title']=="油工工程"&&$s['status']!=2){
                    $yg=1;
                }
                if($s['title']=="木作安装"&&$s['status']==2){
                    $mz=2;
                }
                if($s['title']=="木作安装"&&$s['status']!=2){
                    $mz=1;
                }
                if($s['title']=="竣工保洁"&&$s['status']==2){
                    $bj=2;
                }
                if($s['title']=="竣工保洁"&&$s['status']!=2){
                    $bj=1;
                }

            }
            $this->assign("sd", $sd);
            $this->assign("cc", $cc);
            $this->assign("wg", $wg);
            $this->assign("yg", $yg);
            $this->assign("mz", $mz);
            $this->assign("bj", $bj);
//            dump($sd);
//            dump($cc);
//            dump($wg);
//            dump($yg);
//            dump($mz);
//            dump($bj);
//            die();

            $this->assign("pro", $pro);
            $this->assign("stage", $stage);
            $this->assign('pm', $pm);
            $this->assign('am', $am);
            $this->assign('ds', $ds);
            $this->assign('qc', $qc);

            return $this->fetch();
        }
        if ($request->isAjax()) {
//            $stage = Db::name('project_stage')
//                ->where('status', 2)
//                ->where('project_id', $id)->find();
           // $pid = $stage['id'];
            $page = $request->param("page");
            $res = Db::name('project_stage_log')
                ->page($page, 2)
                ->where('project_id', $id)
                ->order('log_time', 'desc')
                ->select();

            for ($i=0;$i<count($res);$i++) {
                $pictures = Db::name('project_stage_log_picture')->where('stage_log_id', $res[$i]['id'])
                    ->field('log_picture')
                    ->select();
                foreach ($pictures as $index => $pic) {
                    $pictures[$index]['log_picture'] = $qiniu_config['domain'] . '/' . $pic['log_picture'] . $q_thumb;
                }
                $res[$i]['pictures'] = $pictures;
            }

            return json($res);
        }

    }

}